//
//  GQPageViewController.m
//  test
//
//  Created by kepuG on 17/1/22.
//  Copyright © 2017年 text. All rights reserved.
//

#import "GQPageViewController.h"
#import "testViewController.h"

@interface GQPageViewController ()<UIPageViewControllerDelegate ,UIPageViewControllerDataSource>


@end

@implementation GQPageViewController


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(instancetype)initWithTransitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary<NSString *,id> *)options{
    
    self = [super initWithTransitionStyle:style navigationOrientation:navigationOrientation options:options];
    
    if (self) {
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

-(void)setControsArr:(NSArray *)controsArr{
    _controsArr = controsArr;
    testViewController *first = [self viewControllerAtIndex:0];
    NSArray *arr = [NSArray arrayWithObject:first];
    
    [self setViewControllers:arr direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {
        DebugLog(@"==========");
    }];
}

- (testViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.controsArr count] == 0) || (index >= [self.controsArr count])) {
        return nil;
    }
    // 创建一个新的控制器类，并且分配给相应的数据
    testViewController *dataViewController =[[testViewController alloc] init];
    dataViewController.view.backgroundColor = [UIColor grayColor];
    dataViewController.data =[self.controsArr objectAtIndex:index];
    return dataViewController;
}

- (NSUInteger)indexOfViewController:(testViewController *)viewController {
    return [self.controsArr indexOfObject:viewController.data];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self indexOfViewController:(testViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    // 返回的ViewController，将被添加到相应的UIPageViewController对象上。
    // UIPageViewController对象会根据UIPageViewControllerDataSource协议方法，自动来维护次序。
    // 不用我们去操心每个ViewController的顺序问题。
    return [self viewControllerAtIndex:index];
}

// 返回下一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self indexOfViewController:(testViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.controsArr count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
